import time

from selenium.webdriver.common.by import By
#Selenium基础工具类
class SeleniumUtil:

    # 创建浏览器对象
    def __init__(self,driver):
        self.driver=driver

    #打开网页，url=网址
    def sel_get(self,url):
        self.driver.get(url)

    #点击函数，value=网页的XPATH元素
    def sel_click(self,value):
        try:
            self.driver.find_element(By.XPATH, value).click()
        except Exception:
            print("元素点击失败："+value)

    #点击函数，value=网页的XPATH元素
    def sel_get_current_url(self):
        return self.driver.current_url

    def sel_input(self,value,text):
        try:
            self.driver.find_element(By.XPATH,value).send_keys(text)
        except Exception:
            print("元素输入失败："+value)

    def sel_li_elements(self):
        # 定位li元素
        li_elements = self.driver.find_elements(By.XPATH, '//*[@id="wrap"]/div[2]/div[2]/div/div[1]/div[2]/ul/li')

        # 循环处理每个li元素
        for li_element in li_elements:
            # 在每个li元素中查找包含文字“立即沟通”的a标签
            a_element = li_element.find_elements(By.XPATH, ".//a[contains(text(), '立即沟通')]")

            # 如果找到符合条件的a标签
            if a_element:
                # 使用JavaScript点击a标签，而不打开新窗口
                self.driver.execute_script("arguments[0].click();", a_element)











